import {action} from 'mobx';
import {message} from 'antd';
import {Tool, ToolHistory, ToolRoute} from '../utils';
import {loginIn, loginOut} from '../services/Login';

/**
 * 登录模块的Store（状态管理器）
 */
export default class Login {
  //@observable修饰器用来声明action：用来修改state。严格模式下，只能通过action来修改state。
  @action loginIn = (data, store) => {
    return loginIn(data).then((res) => {
      message.success('登录成功');
      Tool.setUser({...res, ...data}); //保存用戶信息
      let {routerMenu, routerFormat} = ToolRoute.formatRouter(res.auth); //根据角色类型获取左侧菜单和路由列表
      store.setRouterMenu(routerMenu); //在根Store中保存左侧菜单
      store.setRouterFormat(routerFormat);
      ToolHistory.href('/menu/demo/order');
    });
  };
  @action loginOut = (store) => {
    return loginOut().then((res) => {
      let {userId, password, check} = Tool.getUser();
      localStorage.clear();
      sessionStorage.clear();
      Tool.setUser({userId, password, check});
      ToolHistory.href('/home/login');
    });
  };
}
